package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.service.impl.ArticlesReplyServiceImpl;
import com.kykj.kykj.vo.ArticlesReplyVo;
import com.kykj.kykj.vo.ArticlesStarVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/articles/")
@CrossOrigin//解决跨域问题
@RestController
public class ArticlesReplyController {
    @Resource
    ArticlesReplyServiceImpl articlesReplyService;

    /**
     * 根据文章id获取评论
     * @param articleId 文章id
     * @return resultEntity
     */
    @GetMapping("getReplyList")
    private ResultEntity getReplyList(
            @RequestParam("articleId") int articleId
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesReplyService.getReplyList(articleId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 新增评论
     * @param articlesReplyVo 评论对象
     * @return resultEntity
     */
    @PostMapping("articlesreply")
    private ResultEntity addReply(@RequestBody ArticlesReplyVo articlesReplyVo){
        ResultEntity resultEntity = new ResultEntity();
        try{

            if(articlesReplyService.addReply(articlesReplyVo)>0){
                System.out.println("当前自增的id是"+articlesReplyVo.getId()+"------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！"+articlesReplyVo.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("articlesreply/{id}")
    private ResultEntity delReply(@PathVariable("id") int id){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesReplyService.delReply(id)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 修改评论
     * @param articlesReplyVo 评论对象
     * @return resultEntity
     */
    @PutMapping("articlesreply")
    private ResultEntity upReply(@RequestBody ArticlesReplyVo articlesReplyVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesReplyService.upReply(articlesReplyVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
}
